package generic;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

public class UtilsTest {
	
	public static AtomicInteger seqIdAtomicInteger = new AtomicInteger(0);
    private static AtomicInteger seqTmCmdIndex = new AtomicInteger(0);

	public static long getPosDataSeqId(long utTime, int gw){
		int subSeq = seqIdAtomicInteger.getAndAdd(1)%10000;
		if (gw > 99) {
			gw = gw%100;
		}
		//utTime * 1000000 + subSeq * 100 + gw;
		//String combineStr = String.format("%d%04d%02d", utTime, subSeq, gw);
		//return Long.valueOf(combineStr);
		return (utTime * 1000000 + subSeq * 100 + gw);
	}
	
    /** 
     * 主键生成规则： 按照10进制合成： utTime(前13) + seqTmCmdIndex(中间4) + gw（最后两位） 共19位(一位代表从0-9的数字).
     * @param utTime
     * @param gw
     * @return
     */
    public static long getTmCmdSeqId(int gw){
        int subSeq = seqTmCmdIndex.getAndAdd(1)%10000;
        if (gw > 99) {
            gw = gw%100;
        }
        long utTime = System.currentTimeMillis();
        return ((utTime) * 1000000 + subSeq * 100 + gw);
    }
    
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Date curDate = new Date();
		long utcTime = curDate.getTime();
		
		System.out.println("UTCTime:" + utcTime);
		long lastTime = System.currentTimeMillis();
		System.out.println("lastTime:" + lastTime);
		for (int i=0; i<20000; i++) {
			//System.out.println("toLong:" + getPosDataSeqId(utcTime,1));
		    long value = getTmCmdSeqId(12);
		    if (i== 0 || i%1000 == 0) {
		        System.out.println("toLong:" +value);
		    }
		}
		System.out.println("UTCTime:" + (System.currentTimeMillis() - lastTime));
	}

}
